﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using System.ComponentModel;
using System;


namespace SalesPortal.Web.Models
{
    public class TablePagination
    {
        public bool DisplayFilterButton {get;set;}
        public bool DisplayPageSizeDropdown {get;set;}
        public bool DisplayPageJumpButton {get;set;}

        public int? PrevPageNo { get; set; }
        public int? NextPageNo { get; set; }
        public int CurrentPageNo { get; set; }
        public int TotalNumberOfPages { get; set; }
        public List<int> RelatedPages { get; set; }

        public TablePagination()
        {
            this.DisplayFilterButton = true;
            this.DisplayPageSizeDropdown = true;
            this.DisplayPageJumpButton = true;
            
        }

        public TablePagination(int recordCount, int pageSize, int? currentPage) : base()
        {
            this.RelatedPages = new List<int>();

            if (pageSize == 0)
            {
                //TODO: Set default in some other configuration setting 
                pageSize = 25; // default page size to 25 records per page
            }

            this.TotalNumberOfPages = (int)Math.Ceiling((decimal)recordCount / (decimal)pageSize);

            if (currentPage.HasValue)
                this.CurrentPageNo = currentPage.Value;
            else
                this.CurrentPageNo = 1;

            if (this.CurrentPageNo > 1)
                PrevPageNo = this.CurrentPageNo - 1;
            else
                PrevPageNo = null;

            if (this.CurrentPageNo < this.TotalNumberOfPages)
                NextPageNo = this.CurrentPageNo + 1;
            else
                NextPageNo = null;

            if (recordCount <= pageSize)
            {
                this.RelatedPages.Add(1);
            }
            else
            {

                int relatedPageCount = 2;

                // Prev Page
                //for (int i = -relatedPageCount; i <= relatedPageCount; i++)
                //{
                //    if (((this.CurrentPageNo + i) >= 1) && 
                //        ((this.CurrentPageNo + i) < this.TotalNumberOfPages))
                //        this.RelatedPages.Add((this.CurrentPageNo + i));
                //    else
                //        relatedPageCount = relatedPageCount + 1;
                //}

                int relatedStartPage = this.CurrentPageNo - relatedPageCount;
                int relatedEndPage = this.CurrentPageNo + relatedPageCount;

                if (((this.CurrentPageNo - relatedPageCount) >= 1) &&
                    ((this.CurrentPageNo + relatedPageCount) < this.TotalNumberOfPages))
                {
                    for (int i = (this.CurrentPageNo - relatedPageCount); i <= (this.CurrentPageNo + relatedPageCount); i++)
                    {
                        this.RelatedPages.Add(i);
                    }
                }
                else
                {
                    // Run out of start page
                    if ((this.CurrentPageNo - relatedPageCount) <= 1)
                    {
                        string x = "";
                        relatedEndPage = relatedEndPage + Math.Abs((this.CurrentPageNo - relatedPageCount) - 1);
                        for (int i = 1; i <= relatedEndPage; i++)
                        {
                            this.RelatedPages.Add(i);
                        }
                    }

                    // Run out of end pages
                    if ((this.CurrentPageNo + relatedPageCount) >= this.TotalNumberOfPages)
                    {
                        relatedStartPage = relatedStartPage -
                            (((this.CurrentPageNo + relatedPageCount) - this.TotalNumberOfPages) + 1);
                        for (int i = relatedStartPage; i < this.TotalNumberOfPages; i++)
                        {
                            this.RelatedPages.Add(i);
                        }
                    }

                    // Run out of both start and end pages

                }
            }

            

        }
        
    }
}